<template>
  <div class="container">
    <div class="search-box">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="用户名称">
          <el-input
            v-model="searchForm.username"
            placeholder="请输入用户名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="评论类型">
          <el-input
            v-model="searchForm.type"
            placeholder="请输入评论类型"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-box clearfix">
      <div class="button-box clearfix">
        <el-button type="danger" @click="handleDeleteList">批量删除</el-button>
      </div>
      <el-table
        border
        :data="commentList"
        style="width: 100%; min-height: 400px"
        @selection-change="handleSelectionChange"
        height="400"
      >
        <el-table-column
          show-overflow-tooltip
          align="center"
          type="selection"
          width="55"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="id"
          label="评论ID"
          width="120"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="username"
          label="用户名称"
          width="180"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="user_id"
          label="用户ID"
          width="120"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="content"
          label="评论内容"
          width="300"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="type"
          label="评论类型"
          width="120"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="type_id"
          label="评论主体ID"
          width="120"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="created_time"
          label="创建时间"
          width="120"
        />
        <el-table-column
          label="操作"
          align="center"
          show-overflow-tooltip
          width="250"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button size="mini" @click="handleView(scope.row)"
              >查看</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 抽屉组件 -->
    <el-drawer
      :title="drawerTitle"
      :visible.sync="drawerVisible"
      size="40%"
      :before-close="handleClose"
      style="overflow-y: auto"
    >
      <el-form :model="form" :rules="rules" ref="form" label-width="120px">
        <el-form-item label="用户名称" prop="username">
          <el-input v-model="form.username" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="用户ID" prop="user_id">
          <el-input v-model="form.user_id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="评论内容" prop="content">
          <el-input
            v-model="form.content"
            type="textarea"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="评论类型" prop="type">
          <el-input v-model="form.type" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="评论主体ID" prop="type_id">
          <el-input v-model="form.type_id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="创建数" prop="created_time">
          <el-input v-model="form.created_time" :disabled="true"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">关闭</el-button>
        </span>
      </template>
    </el-drawer>
  </div>
</template>

<script>
import { mapGetters } from "vuex";

export default {
  name: "CommentManagement",

  data() {
    return {
      searchForm: {
        username: "",
        type: "",
      },
      commentList: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      form: {
        username: "",
        user_id: "",
        content: "",
        type: "",
        type_id: "",
        created_time: 0,
      },
      drawerTitle: "查看评论",
      drawerVisible: false,
      isView: true,
      rules: {},
      multipleSelection: [],
    };
  },
  computed: {
    ...mapGetters(["tableColumnLabels"]),
  },
  created() {
    this.fetchCommentList();
  },
  methods: {
    fetchCommentList() {
      // 模拟异步请求
      let res = {
        data: [
          {
            id: 1,
            username: "张三",
            user_id: 1001,
            content: "这个商品很不错",
            type: "商品评论",
            type_id: 2001,
            created_time: 5,
          },
        ],
      };
      this.commentList = res.data;
      this.total = res.data.length;
    },
    handleSearch() {
      this.currentPage = 1;
      this.fetchCommentList();
    },
    resetSearch() {
      this.searchForm = {
        username: "",
        type: "",
      };
      this.fetchCommentList();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchCommentList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchCommentList();
    },
    handleView(row) {
      this.isView = true;
      this.drawerTitle = "查看评论";
      this.form = { ...row };
      this.drawerVisible = true;
    },
    handleDelete(row) {
      // 删除逻辑
      // 模拟删除
      this.$confirm("确定要删除该评论吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$message({
          type: "success",
          message: "评论删除成功！",
        });
        this.fetchCommentList();
      });
    },
    handleClose() {
      this.drawerVisible = false;
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection;
    },
    handleDeleteList() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning("请选择需要删除的评论");
        return;
      }
      this.$confirm("确定要删除选中的评论吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const ids = this.multipleSelection.map((item) => item.id);
          console.log("批量删除评论 ID:", ids);
          this.$message.success("批量删除成功");
          this.fetchCommentList();
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
}

.search-box {
  margin-bottom: 20px;
}

.search-form {
  display: flex;
  align-items: center;
}

.table-box {
  .el-table {
    margin-bottom: 20px;
  }
}

.avatar-uploader {
  width: 130px;
  height: 130px;
}
::v-deep .avatar-uploader .el-upload {
  width: 130px;
  height: 130px;
  border: 1px dashed #333333;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  position: relative;
  width: 130px;
  height: 130px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 130px;
  height: 130px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 130px;
  height: 130px;
  display: block;
}
.position {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -68%);
}
.product-image {
  border-radius: 4px;
}
</style>
